package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.example.zzl.R;
import com.example.zzl.custom.DividerItemDecoration;
import com.example.zzl.custom.SyLinearLayoutManager;
import com.example.zzl.databinding.HomeItemivBinding;
import com.example.zzl.databinding.HomeItemlvBinding;
import com.example.zzl.databinding.HomeItemvpBinding;
import com.example.zzl.model.bean.Home;
import com.example.zzl.viewmodel.HomeFragment.RecommendPresenter;

import java.util.List;

/**
 * Created by ligg on 2017/2/8.
 */

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder> implements ViewPager.OnPageChangeListener, View.OnTouchListener {

    private RecommendPresenter presenter;
    private LayoutInflater inflater;
    private Context context;
    private List<Object> list;
    private HomeItemvpBinding vpBinding;


    public RecommendAdapter(Context context, List<Object> list, RecommendPresenter recommendPresenter) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
        presenter = recommendPresenter;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.home_itemvp, parent, false);
                viewHolder = new ViewHolder(view);
                HomeItemvpBinding vpDb = DataBindingUtil.bind(view);
                viewHolder.setVpBinding(vpDb);
                break;

            case 1:
                view = inflater.inflate(R.layout.home_itemlv, parent, false);
                viewHolder = new ViewHolder(view);
                HomeItemlvBinding lvDb = DataBindingUtil.bind(view);
                viewHolder.setLvBinding(lvDb);
                break;

            case 2:
                view = inflater.inflate(R.layout.home_itemiv, parent, false);
                viewHolder = new ViewHolder(view);
                HomeItemivBinding ivDb = DataBindingUtil.bind(view);
                viewHolder.setIvBinding(ivDb);
                break;

            default:
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Object o = list.get(position);
        switch (getItemViewType(position)) {
            case 0:
                Home.OrderCommodity orderCommodity = (Home.OrderCommodity) o;
                vpBinding = holder.getVpBinding();
                vpBinding.setOrderCommodity(orderCommodity);
                vpBinding.rv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
                vpBinding.rv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL_LIST));
                RecommendVpRvAdapter vpRvAdapter = new RecommendVpRvAdapter(context, orderCommodity.getResult());
                vpRvAdapter.setOnItemClickLitener(presenter);
                vpBinding.rv.setAdapter(vpRvAdapter);
                vpBinding.executePendingBindings();
                break;

            case 1:
                Home.Special special = (Home.Special) o;
                HomeItemlvBinding lvBinding = holder.getLvBinding();
                lvBinding.setSpecial(special);
                RecommendLvAdapter lvAdapter = new RecommendLvAdapter(context, special.result);
                lvAdapter.setOnItemClickLitener(presenter);
                SyLinearLayoutManager layout = new SyLinearLayoutManager(context);
                layout.setOrientation(LinearLayoutManager.VERTICAL);
                lvBinding.rv.setLayoutManager(layout);
                lvBinding.rv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
                lvBinding.rv.setAdapter(lvAdapter);
                lvBinding.executePendingBindings();
                break;

            case 2:
                Home.BrandExclusive brandExclusive = (Home.BrandExclusive) o;
                HomeItemivBinding ivBinding = holder.getIvBinding();
                ivBinding.setBrandExclusive(brandExclusive);
                RecommendRvAdapter rvAdapter = new RecommendRvAdapter(context, brandExclusive.result, presenter);
                SyLinearLayoutManager layout1 = new SyLinearLayoutManager(context);
                layout1.setOrientation(LinearLayoutManager.VERTICAL);
                ivBinding.rv.setLayoutManager(layout1);
                ivBinding.rv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
                ivBinding.rv.setAdapter(rvAdapter);
                ivBinding.executePendingBindings();
                break;

            default:
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        Object o = list.get(position);
        if (o instanceof Home.OrderCommodity) {
            return 0;
        } else if (o instanceof Home.Special) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        private HomeItemvpBinding vpBinding;
        private HomeItemlvBinding lvBinding;
        private HomeItemivBinding ivBinding;

        public HomeItemvpBinding getVpBinding() {
            return vpBinding;
        }

        public void setVpBinding(HomeItemvpBinding vpBinding) {
            this.vpBinding = vpBinding;
        }

        public HomeItemlvBinding getLvBinding() {
            return lvBinding;
        }

        public void setLvBinding(HomeItemlvBinding lvBinding) {
            this.lvBinding = lvBinding;
        }

        public HomeItemivBinding getIvBinding() {
            return ivBinding;
        }

        public void setIvBinding(HomeItemivBinding ivBinding) {
            this.ivBinding = ivBinding;
        }

        public ViewHolder(View itemView) {
            super(itemView);

        }
    }
}
